<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap>
	<typeAlias type="${tableClass.basePackageName}.model.${tableClass.className}" alias="${tableClass.className}"/>
	<typeAlias type="${tableClass.basePackageName}.dto.${tableClass.className}Dto" alias="${tableClass.className}Dto"/>
	<#--
	<resultMap id="${tableClass.className[0]?lower_case + tableClass.className[1..]}Result" class="${tableClass.className}">
	<#list tableClass.columns as item>
		<result property="${item.classFieldName}" column="${item.columnName}"></result>
	</#list>
	</resultMap>
	-->
	
	<sql id="select${tableClass.className}">	
		<![CDATA[
			SELECT * FROM ${tableClass.tableName}
		]]>
	</sql>
	
	<sql id="select${tableClass.className}ByDto">
		<include refid="select${tableClass.className}"/>
		<dynamic prepend="WHERE">
		</dynamic>		
		<isNotEmpty property="sort">
			order by $sort$ $dir$
		</isNotEmpty>
	</sql>
	
	<select id="get${tableClass.className}List" resultClass="${tableClass.className}">
		<include refid="select${tableClass.className}"/>
	</select>
	
	<select id="get${tableClass.className}" parameterClass="Long" resultClass="${tableClass.className}">
		<include refid="select${tableClass.className}"/>
		WHERE ${tableClass.pkColumnName}=#value#
	</select>
	
	<select id="countFind${tableClass.className}WithPg" resultClass="java.lang.Integer" parameterClass="${tableClass.className}Dto">
	    SELECT COUNT(*) FROM (<include refid="select${tableClass.className}ByDto"/>)
	</select>
	
	<select id="find${tableClass.className}WithPg" resultClass="${tableClass.className}" parameterClass="${tableClass.className}Dto">
		SELECT * FROM (SELECT ROW_.*, ROWNUM RN FROM (
		<include refid="select${tableClass.className}ByDto"/>
		) ROW_ WHERE #end# >= ROWNUM ) WHERE RN > #start#
	</select>
	
	<insert id="add${tableClass.className}"  parameterClass="${tableClass.className}">
		<selectKey keyProperty="${tableClass.pkFieldName}" resultClass="long" type="pre">
			<![CDATA[
				SELECT SEQ_${tableClass.tableName}_${tableClass.pkColumnName}.NEXTVAL FROM DUAL
			]]>
		</selectKey>
		<![CDATA[
			INSERT INTO ${tableClass.tableName}(
			<#list tableClass.columns as item>
				${item.columnName}<#if item_index + 1 != tableClass.columns?size>,</#if>
			</#list>
			)
			VALUES(
			<#list tableClass.columns as item>
				#${item.classFieldName}#<#if item_index + 1 != tableClass.columns?size>,</#if>
			</#list>
			)
		]]>
	</insert>
	
	<update id="update${tableClass.className}" parameterClass="${tableClass.className}">
		<![CDATA[
			UPDATE ${tableClass.tableName} SET
			<#list tableClass.columns as item>
				${item.columnName}=#${item.classFieldName}#<#if item_index + 1 != tableClass.columns?size>,</#if>
			</#list>
			WHERE ${tableClass.pkColumnName}=#${tableClass.pkFieldName}#
		]]>
	</update>
		
	<update id="update${tableClass.className}Selective" parameterClass="${tableClass.className}">
		UPDATE ${tableClass.tableName}			
	    <dynamic prepend="SET" >
		<#list tableClass.columns as item>
	    	<isNotNull prepend="," property="${item.classFieldName}" >
				${item.columnName}=#${item.classFieldName}#
	    	</isNotNull>
		</#list>
	    </dynamic>
		WHERE ${tableClass.pkColumnName}=#${tableClass.pkFieldName}#
	</update>
	
	<delete id="remove${tableClass.className}" parameterClass="Long">
		<![CDATA[
			DELETE FROM ${tableClass.tableName} WHERE ${tableClass.pkColumnName}=#value#
		]]>
	</delete>
</sqlMap>